package cn.demo.client.api;

import cn.demo.client.domain.login.UserLoginBody;
import cn.demo.client.service.IApiLoginService;
import cn.demo.common.core.domain.R;
import cn.demo.framework.web.service.TokenService;
import cn.demo.weixin.ma.service.WxMaLoginService;
import cn.demo.weixin.ma.utils.WxMaUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author xtjun
 * @description 授权认证API
 * @date 2022/09/01
 */
@Tag(name = "认证授权")
@RestController
@RequestMapping("/api/login")
public class AuthApi {

    @Resource
    private IApiLoginService apiLoginService;
    @Resource
    private WxMaLoginService wxMaLoginService;

    @Resource
    private TokenService tokenService;

    @PostMapping("/pass")
    @Operation(summary = "客户端用户密码登录")
    public R<String> userLogin(@RequestBody UserLoginBody loginBody) {
        return R.ok(apiLoginService.userLogin(loginBody));
    }

    @PostMapping("/ma")
    @Operation(summary = "客户端小程序登录")
    public R<String> wxmaLogin(@RequestBody Map<String,String> param, HttpServletRequest request) {
        return R.ok(tokenService.createToken(wxMaLoginService.wxmaLogin(WxMaUtil.getAppId(request),param.get("code"))));
    }

    @PostMapping("/maTemporary")
    @Operation(summary = "客户端小程序临时登录")
    public R<String> wxmaLoginTemporary(@RequestBody Map<String,String> param, HttpServletRequest request) {
        return R.ok(tokenService.createToken(wxMaLoginService.wxmaLoginTemporary(WxMaUtil.getAppId(request),param.get("code"))));
    }

    @PostMapping("/maByPhone")
    @Operation(summary = "客户端小程序手机授权登录")
    public R<String> wxmaloginByPhone(@RequestBody Map<String,String> param,HttpServletRequest request) {
        return R.ok(tokenService.createToken(wxMaLoginService.wxmaloginByPhone(WxMaUtil.getAppId(request),param.get("code"))));
    }


}
